//
//  GRXCardViewController.m
//  1-MushroomStreet-自定义tabbar
//
//  Created by xmg on 16/1/16.
//  Copyright © 2016年 grx. All rights reserved.
//

#import "GRXCardViewController.h"
#import <SVProgressHUD/SVProgressHUD.h>
@interface GRXCardViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *cardImageView;
@property (weak, nonatomic) IBOutlet UIView *cardContentView;

@end

@implementation GRXCardViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //创建二维码图片
    [self creatQRCodeImage];
    
    //设置二维码图片保存
    [self SetSaveBarButtonItem];
}

- (void)SetSaveBarButtonItem{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStyleDone target:self action:@selector(saveMyQRCode)];
}

- (void)saveMyQRCode{
    //1.开启一个位图上下文(和contentView的尺寸一样大)
    UIGraphicsBeginImageContextWithOptions(self.cardContentView.frame.size, NO, 0);
    
    //2.把控制器view绘制到上下文中
    /*
     控制器的view是不能直接绘制到上下文当中的
     想要把控制器的view的内容绘制到上下文当中,就必须使用渲染的方式才能够绘制
     */
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [self.cardContentView.layer renderInContext:ctx];
    
    //3.从当前的上下文当中生成一张图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    //4.关闭上下文
    UIGraphicsGetCurrentContext();
    
    //5.把图片保存至相册
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    [SVProgressHUD showSuccessWithStatus:@"保存成功"];
}

#pragma mark ---生成二维码图片
- (void)creatQRCodeImage{
    //1.创建滤镜
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    
    //2.恢复默认设置
    [filter setDefaults];
    
    //3.设置数据
    NSString *qrcodeInfo = @"http://www.mogujie.com";
    
    NSData *qrcodeInfoData = [qrcodeInfo dataUsingEncoding:NSUTF8StringEncoding];
    
    if (qrcodeInfoData == nil) {
        return;
    }
    
    [filter setValue:qrcodeInfoData forKeyPath:@"inputMessage"];
    
    //4.获取生成的图片
    CIImage *outPutImage = [filter outputImage];
    
    if (outPutImage == nil) {
        return;
    }
    
    //5.设置图片
    UIImage *qrcodeImage = [self scaleImage:outPutImage];
    
    UIImage *image = [self drawIconToQrcode:qrcodeImage withImageName:@"cardIcon"];
    
    self.cardImageView.image = image;
    
}

//把用户头像画到二维码图片中间
- (UIImage *)drawIconToQrcode:(UIImage *)qrcodeImage withImageName:(NSString *)iconName{
    //1.开启上下文
    UIGraphicsBeginImageContext(qrcodeImage.size);
    
    //2.将图片画到上下文中
    [qrcodeImage drawInRect:CGRectMake(0, 0, qrcodeImage.size.width, qrcodeImage.size.height)];
    
    //3.根据图表名称生成图片,并且将图片画到上下文中
    UIImage *iconImage = [UIImage imageNamed:iconName];
    
    if (iconImage == nil) {
        return nil;
    }
    
    //3.1 设置icon图片的宽度和高度
    CGFloat width = 60;
    CGFloat height = 60;
    CGFloat x = (qrcodeImage.size.width - width) * 0.5;
    CGFloat y = (qrcodeImage.size.height - height) * 0.5;
    [iconImage drawInRect:CGRectMake(x, y, width, height)];
    
    //4.获取图片
    UIImage *drawImage = UIGraphicsGetImageFromCurrentImageContext();
    
    //5.关闭上下文
    UIGraphicsEndImageContext();
    
    return drawImage;
}


//缩放图片(为了让生成的二维码图片更加清晰)
- (UIImage *)scaleImage:(CIImage *)ciImage{
    //1.创建放大的系数
    CGAffineTransform  tranform = CGAffineTransformMakeScale(10, 10);
    
    //2.根据放大系数放大图片
    CIImage *newImage = [ciImage imageByApplyingTransform:tranform];
    
    //3.返回生成好的图片
    return [UIImage imageWithCIImage:newImage];
}

@end
